package de.tub.pong.objects;

import processing.core.PApplet;

public class Racket {

	// statics
	private static final float racketHeight = 100;
	private static final float racketWidth = 15;
	private static final float racketBorderDistance = 15;
	private static final float racketSpeed = 7;

	// variable
	private float x;
	private float y;

	private float oldX; // X Koordinate one Frame ago

	private PApplet parent;

	public Racket(PApplet parent, int playerNumber) {
		this.parent = parent;
		this.y = (parent.height - racketHeight) / 2;

		if (playerNumber == 1) {
			this.x = racketBorderDistance;
		} else {
			this.x = parent.width - (racketBorderDistance + racketWidth);
		}

	}

	public void setX(float x) {
		this.oldX = this.x;
		this.x = x;
		if (getLeftX() <= racketBorderDistance) {
			this.x = racketBorderDistance;
		} else if (getRightX() + racketBorderDistance >= parent.width) {
			this.x = parent.width - (racketBorderDistance + racketWidth);
		}
	}

	public void setCenterX(float x) {
		this.setX(x - racketWidth / 2);
	}

	public void setY(float y) {
		this.y = y;
		if (getUpperY() <= 0) {
			this.y = 0;
		}
		if (getLowerY() >= parent.height) {
			this.y = parent.height - racketHeight;
		}
	}

	public void setCenterY(float y) {
		this.setY(y - racketHeight / 2);
	}

	public float getLeftX() {
		return x;
	}

	public float getRightX() {
		return x + racketWidth;
	}

	public float getCurrentSpeedX() {
		// TODO Richtung ist auch wichtig
		return Math.abs(this.oldX - this.x);
	}

	public float getUpperY() {
		return y;
	}

	public float getLowerY() {
		return y + racketHeight;
	}

	public void moveUp() {
		this.setY(this.y - racketSpeed);
	}

	public void moveDown() {
		this.setY(this.y + racketSpeed);
	}

	public void moveLeft() {
		this.setX(this.x - racketSpeed);
	}

	public void moveRight() {
		this.setX(this.x + racketSpeed);
	}

	public void update(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public float calcDistance(float ballX) {
		return Math.min(Math.abs(ballX - getLeftX()), Math.abs(ballX
				- getRightX()));
	}

	public void draw() {
		parent.rect(this.x, this.y, racketWidth, racketHeight);

	}

}
